#!/bin/bash

source $HOME/.bashrc && conda activate lxb39 && cd $PROJECTS/MoE-LLaVA

gpu_list="${CUDA_VISIBLE_DEVICES:-0}"
IFS=',' read -ra GPULIST <<< "$gpu_list"
CHUNKS=${#GPULIST[@]}

CONV="phi"
CKPT_NAME="moe-llava-clip-336-phi-2.7b-3rd-sft-moe-reproduce"
CKPT="$OUTPUTS/MoE-LLaVA/${CKPT_NAME}"
EVAL="eval_files"

mkdir -p ${EVAL}/MME/answers/${CKPT_NAME}
for IDX in $(seq 0 $(($CHUNKS - 1))); do
    deepspeed --include localhost:${GPULIST[$IDX]} --master_port $((20795 + ${GPULIST[${IDX}]})) moellava/eval/model_vqa_loader.py \
        --model-path ${CKPT} \
        --question-file ${EVAL}/MME/llava_mme.jsonl \
        --image-folder ${EVAL}/MME/MME_Benchmark_release_version \
        --answers-file ${EVAL}/MME/answers/${CKPT_NAME}/${CHUNKS}_${IDX}.jsonl \
        --num-chunks ${CHUNKS} \
        --chunk-idx ${IDX} \
        --temperature 0 \
        --conv-mode ${CONV} &
done
wait

output_file=${EVAL}/MME/answers/${CKPT_NAME}/${CKPT_NAME}.jsonl
> "$output_file"

for IDX in $(seq 0 $((CHUNKS-1))); do
    cat ${EVAL}/MME/answers/${CKPT_NAME}/${CHUNKS}_${IDX}.jsonl >> "$output_file"
done

cd ${EVAL}/MME

python convert_answer_to_mme.py --experiment $CKPT_NAME

cd eval_tool

python calculation.py --results_dir answers/$CKPT_NAME

